package com.example.sunmiv1.adapter;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.AllocationGoodsInfo;
import com.example.sunmiv1.entity.DepositGoodsEditInfo;
import com.example.sunmiv1.entity.DepositGoodsInfo;
import com.example.sunmiv1.entity.GoodBean;
import com.example.sunmiv1.entity.GoodsStoreRetreatInfo;
import com.example.sunmiv1.entity.LossGoodsInfo;
import com.example.sunmiv1.entity.OnlineOrderBean;
import com.example.sunmiv1.entity.OrderDetailInfo;
import com.example.sunmiv1.entity.ReportLossGoodBean;
import com.example.sunmiv1.entity.SerialDetailInfo;
import com.example.sunmiv1.entity.WarehousingDetailInfo;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.content.ContentValues.TAG;

public class StringCodeDetailAdapter extends BaseAdapter {

    private List<?> list;
    private ViewHolder viewHolder;
    private Operation operation;
    private List<String> judgeList;

    public StringCodeDetailAdapter(List<?> list) {
        this.list = list;
    }

    public void setList(List<?> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void setJudgeList(List<String> judgeList) {
        this.judgeList = judgeList;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        position = list.size() - position - 1;
//        if (convertView == null) {
        convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.string_code_detail_adapter, null);
        viewHolder = new ViewHolder(convertView);
//            convertView.setTag(viewHolder);
//        } else {
//            viewHolder = (ViewHolder) convertView.getTag();
//        }
        Log.e(TAG, "getView: 數組的長度=====" + list.size());
        if (list.get(position) instanceof GoodBean.DataBean.GoodsBean.SerialBean) {
            GoodBean.DataBean.GoodsBean.SerialBean bean = (GoodBean.DataBean.GoodsBean.SerialBean) list.get(position);
            viewHolder.codeDetailCode.setEnabled(false);
            viewHolder.codeDetailCode1.setEnabled(false);
            viewHolder.codeDetailCode2.setEnabled(false);
            viewHolder.codeDetailCode.setText(bean.getMain_serial_number());
            viewHolder.codeDetailCode1.setText(bean.getAuxiliary_serial_number_one());
            viewHolder.codeDetailCode2.setText(bean.getAuxiliary_serial_number_two());
            viewHolder.codeDetailSave.setVisibility(View.GONE);
            viewHolder.codeDetailEdit.setVisibility(View.GONE);
        } else if (list.get(position) instanceof ReportLossGoodBean.DataBean.ListBean.SerialBean) {
            ReportLossGoodBean.DataBean.ListBean.SerialBean bean = (ReportLossGoodBean.DataBean.ListBean.SerialBean) list.get(position);
            viewHolder.codeDetailCode.setEnabled(false);
            viewHolder.codeDetailCode1.setEnabled(false);
            viewHolder.codeDetailCode2.setEnabled(false);
            viewHolder.codeDetailCode.setText(bean.getMain_serial_number());
            viewHolder.codeDetailCode1.setText(bean.getAuxiliary_serial_number_one());
            viewHolder.codeDetailCode2.setText(bean.getAuxiliary_serial_number_two());
            viewHolder.codeDetailSave.setVisibility(View.GONE);
        } else if (list.get(position) instanceof SerialDetailInfo.DataBean.ListBean) {
            SerialDetailInfo.DataBean.ListBean bean = (SerialDetailInfo.DataBean.ListBean) list.get(position);
            viewHolder.codeDetailCode.setEnabled(false);
            viewHolder.codeDetailCode1.setEnabled(false);
            viewHolder.codeDetailCode2.setEnabled(false);
            viewHolder.codeDetailEdit.setVisibility(View.GONE);
            viewHolder.codeDetailDelete.setVisibility(View.GONE);
            viewHolder.codeDetailSave.setVisibility(View.GONE);
            viewHolder.codeDetailCode.setText(bean.getMain_serial_number());
            viewHolder.codeDetailCode1.setText(bean.getAuxiliary_serial_number_one());
            viewHolder.codeDetailCode2.setText(bean.getAuxiliary_serial_number_two());
        } else if (list.get(position) instanceof LossGoodsInfo.DataBean.SerialBean) {
            LossGoodsInfo.DataBean.SerialBean bean = (LossGoodsInfo.DataBean.SerialBean) list.get(position);
            viewHolder.codeDetailCode.setEnabled(false);
            viewHolder.codeDetailCode1.setEnabled(false);
            viewHolder.codeDetailCode2.setEnabled(false);
            viewHolder.codeDetailSave.setVisibility(View.GONE);
            viewHolder.codeDetailCode.setText(bean.getMain_serial_number());
            viewHolder.codeDetailCode1.setText(bean.getAuxiliary_serial_number_one());
            viewHolder.codeDetailCode2.setText(bean.getAuxiliary_serial_number_two());
        } else if (list.get(position) instanceof DepositGoodsInfo.DataBean.ListBean.SerialBean) {
            DepositGoodsInfo.DataBean.ListBean.SerialBean bean = (DepositGoodsInfo.DataBean.ListBean.SerialBean) list.get(position);
            viewHolder.codeDetailCode.setEnabled(false);
            viewHolder.codeDetailCode1.setEnabled(false);
            viewHolder.codeDetailCode2.setEnabled(false);
            viewHolder.codeDetailSave.setVisibility(View.GONE);
            viewHolder.codeDetailCode.setText(bean.getMain_serial_number());
            viewHolder.codeDetailCode1.setText(bean.getAuxiliary_serial_number_one());
            viewHolder.codeDetailCode2.setText(bean.getAuxiliary_serial_number_two());
        } else if (list.get(position) instanceof DepositGoodsEditInfo.DataBean.SerialListBean) {
            DepositGoodsEditInfo.DataBean.SerialListBean bean = (DepositGoodsEditInfo.DataBean.SerialListBean) list.get(position);
            viewHolder.codeDetailCode.setEnabled(false);
            viewHolder.codeDetailCode1.setEnabled(false);
            viewHolder.codeDetailCode2.setEnabled(false);
            if (bean.getEdit_status() == 1) {
                viewHolder.codeDetailEdit.setVisibility(View.GONE);
                viewHolder.codeDetailDelete.setVisibility(View.GONE);
                viewHolder.codeDetailSave.setVisibility(View.GONE);
            } else if (bean.getEdit_status() == 2) {
                viewHolder.codeDetailEdit.setVisibility(View.VISIBLE);
                viewHolder.codeDetailDelete.setVisibility(View.GONE);
                viewHolder.codeDetailSave.setVisibility(View.GONE);
            } else {
                viewHolder.codeDetailEdit.setVisibility(View.VISIBLE);
                viewHolder.codeDetailDelete.setVisibility(View.VISIBLE);
                viewHolder.codeDetailSave.setVisibility(View.GONE);
            }
            viewHolder.codeDetailCode.setText(bean.getMain_serial_number());
            viewHolder.codeDetailCode1.setText(bean.getAuxiliary_serial_number_one());
            viewHolder.codeDetailCode2.setText(bean.getAuxiliary_serial_number_two());
        } else if (list.get(position) instanceof GoodsStoreRetreatInfo.DataBean.SerialListBean) {
            GoodsStoreRetreatInfo.DataBean.SerialListBean bean = (GoodsStoreRetreatInfo.DataBean.SerialListBean) list.get(position);
            viewHolder.codeDetailCode.setEnabled(false);
            viewHolder.codeDetailCode1.setEnabled(false);
            viewHolder.codeDetailCode2.setEnabled(false);
            viewHolder.codeDetailSave.setVisibility(View.GONE);
            viewHolder.codeDetailEdit.setVisibility(View.GONE);
            viewHolder.codeDetailCode.setText(bean.getMain_serial_number());
            viewHolder.codeDetailCode1.setText(bean.getAuxiliary_serial_number_one());
            viewHolder.codeDetailCode2.setText(bean.getAuxiliary_serial_number_two());
        } else if (list.get(position) instanceof AllocationGoodsInfo.DataBean.SerialListBean) {
            AllocationGoodsInfo.DataBean.SerialListBean bean = (AllocationGoodsInfo.DataBean.SerialListBean) list.get(position);
            viewHolder.codeDetailCode.setEnabled(false);
            viewHolder.codeDetailCode1.setEnabled(false);
            viewHolder.codeDetailCode2.setEnabled(false);
            viewHolder.codeDetailSave.setVisibility(View.GONE);
            viewHolder.codeDetailEdit.setVisibility(View.GONE);
            viewHolder.codeDetailCode.setText(bean.getMain_serial_number());
            viewHolder.codeDetailCode1.setText(bean.getAuxiliary_serial_number_one());
            viewHolder.codeDetailCode2.setText(bean.getAuxiliary_serial_number_two());
        } else if (list.get(position) instanceof WarehousingDetailInfo.DataBean.SerialListBean) {
            WarehousingDetailInfo.DataBean.SerialListBean bean = (WarehousingDetailInfo.DataBean.SerialListBean) list.get(position);
            viewHolder.codeDetailCode.setEnabled(false);
            viewHolder.codeDetailCode1.setEnabled(false);
            viewHolder.codeDetailCode2.setEnabled(false);
            viewHolder.codeDetailSave.setVisibility(View.GONE);
            viewHolder.codeDetailEdit.setVisibility(View.GONE);
            viewHolder.codeDetailDelete.setVisibility(View.GONE);
            if (bean.isCheck()) {//未選中
                viewHolder.codeDetailChoice.setVisibility(View.VISIBLE);
            } else {
                viewHolder.codeDetailSelected.setVisibility(View.VISIBLE);
            }
            viewHolder.codeDetailCode.setText(bean.getMain_serial_number());
            viewHolder.codeDetailCode1.setText(bean.getAuxiliary_serial_number_one());
            viewHolder.codeDetailCode2.setText(bean.getAuxiliary_serial_number_two());
        } else if (list.get(position) instanceof OrderDetailInfo.DataBean.SerialListBean) {
            OrderDetailInfo.DataBean.SerialListBean bean = (OrderDetailInfo.DataBean.SerialListBean) list.get(position);
            viewHolder.codeDetailCode.setEnabled(false);
            viewHolder.codeDetailCode1.setEnabled(false);
            viewHolder.codeDetailCode2.setEnabled(false);
            viewHolder.codeDetailSave.setVisibility(View.GONE);
            viewHolder.codeDetailEdit.setVisibility(View.GONE);
            viewHolder.codeDetailCode.setText(bean.getMain_serial_number());
            viewHolder.codeDetailCode1.setText(bean.getAuxiliary_serial_number_one());
            viewHolder.codeDetailCode2.setText(bean.getAuxiliary_serial_number_two());
        } else if (list.get(position) instanceof OnlineOrderBean.OrderBean.GoodInfoBean.SerialListBean) {
            OnlineOrderBean.OrderBean.GoodInfoBean.SerialListBean bean = (OnlineOrderBean.OrderBean.GoodInfoBean.SerialListBean) list.get(position);
            viewHolder.codeDetailCode.setEnabled(false);
            viewHolder.codeDetailCode1.setEnabled(false);
            viewHolder.codeDetailCode2.setEnabled(false);
            viewHolder.codeDetailSave.setVisibility(View.GONE);
            viewHolder.codeDetailEdit.setVisibility(View.GONE);
            viewHolder.codeDetailDelete.setVisibility(View.GONE);
            viewHolder.codeDetailModify.setVisibility(View.VISIBLE);
            viewHolder.codeDetailRecord.setVisibility(View.VISIBLE);
            viewHolder.codeDetailCode.setText(bean.getMain_serial_number());
            viewHolder.codeDetailCode1.setText(bean.getAuxiliary_serial_number_one());
            viewHolder.codeDetailCode2.setText(bean.getAuxiliary_serial_number_two());
        }
        final int index = position;
        final LinearLayout edit = viewHolder.codeDetailEdit;
        final LinearLayout delete = viewHolder.codeDetailDelete;
        final LinearLayout save = viewHolder.codeDetailSave;
        final LinearLayout choice = viewHolder.codeDetailChoice;
        final LinearLayout selected = viewHolder.codeDetailSelected;
        final EditText code = viewHolder.codeDetailCode;
        final EditText code1 = viewHolder.codeDetailCode1;
        final EditText code2 = viewHolder.codeDetailCode2;
        final TextView modify = viewHolder.codeDetailModify;
        final TextView record = viewHolder.codeDetailRecord;
        choice.setOnClickListener(v -> {
//            WarehousingDetailInfo.DataBean.SerialListBean bean = (WarehousingDetailInfo.DataBean.SerialListBean) list.get(position);
//            bean.setCheck(!bean.isCheck());
//            notifyDataSetChanged();
            operation.opera(3, index);
        });
        selected.setOnClickListener(v -> {
//            WarehousingDetailInfo.DataBean.SerialListBean bean = (WarehousingDetailInfo.DataBean.SerialListBean) list.get(position);
//            bean.setCheck(!bean.isCheck());
//            notifyDataSetChanged();
            operation.opera(3, index);
        });
        edit.setOnClickListener(v -> {
            code.setEnabled(true);
            code1.setEnabled(true);
            code2.setEnabled(true);
            edit.setVisibility(View.GONE);
            delete.setVisibility(View.GONE);
            save.setVisibility(View.VISIBLE);
            operation.opera(2, index);
        });
        delete.setOnClickListener(v -> {
            operation.opera(1, index);
        });
        modify.setOnClickListener(v -> operation.opera(4, index));
        record.setOnClickListener(v -> operation.opera(5, index));
        save.setOnClickListener(v -> {
            if (code.getText().toString().equals(code1.getText().toString().trim())) {
                Toast.makeText(parent.getContext(), "串碼重複", Toast.LENGTH_SHORT).show();
                return;
            }
            if (code.getText().toString().equals(code2.getText().toString().trim())) {
                Toast.makeText(parent.getContext(), "串碼重複", Toast.LENGTH_SHORT).show();
                return;
            }
            if (!TextUtils.isEmpty(code1.getText().toString()) && code1.getText().toString().equals(code2.getText().toString().trim())) {
                Toast.makeText(parent.getContext(), "串碼重複", Toast.LENGTH_SHORT).show();
                return;
            }
            if (list.get(index) instanceof ReportLossGoodBean.DataBean.ListBean.SerialBean) {
                ReportLossGoodBean.DataBean.ListBean.SerialBean b = (ReportLossGoodBean.DataBean.ListBean.SerialBean) list.get(index);
                if (judgeList.indexOf(b.getMain_serial_number()) != -1) {
                    judgeList.remove(judgeList.indexOf(b.getMain_serial_number()));
                }
                if (judgeList.indexOf(b.getAuxiliary_serial_number_one()) != -1) {
                    judgeList.remove(judgeList.indexOf(b.getAuxiliary_serial_number_one()));
                }
                if (judgeList.indexOf(b.getAuxiliary_serial_number_two()) != -1) {
                    judgeList.remove(judgeList.indexOf(b.getAuxiliary_serial_number_two()));
                }
            } else if (list.get(index) instanceof DepositGoodsInfo.DataBean.ListBean.SerialBean) {
                DepositGoodsInfo.DataBean.ListBean.SerialBean b = (DepositGoodsInfo.DataBean.ListBean.SerialBean) list.get(index);
                if (judgeList.indexOf(b.getMain_serial_number()) != -1) {
                    judgeList.remove(judgeList.indexOf(b.getMain_serial_number()));
                }
                if (judgeList.indexOf(b.getAuxiliary_serial_number_one()) != -1) {
                    judgeList.remove(judgeList.indexOf(b.getAuxiliary_serial_number_one()));
                }
                if (judgeList.indexOf(b.getAuxiliary_serial_number_two()) != -1) {
                    judgeList.remove(judgeList.indexOf(b.getAuxiliary_serial_number_two()));
                }
            } else if (list.get(index) instanceof LossGoodsInfo.DataBean.SerialBean) {
                LossGoodsInfo.DataBean.SerialBean b = (LossGoodsInfo.DataBean.SerialBean) list.get(index);
                if (judgeList.indexOf(b.getMain_serial_number()) != -1) {
                    judgeList.remove(judgeList.indexOf(b.getMain_serial_number()));
                }
                if (judgeList.indexOf(b.getAuxiliary_serial_number_one()) != -1) {
                    judgeList.remove(judgeList.indexOf(b.getAuxiliary_serial_number_one()));
                }
                if (judgeList.indexOf(b.getAuxiliary_serial_number_two()) != -1) {
                    judgeList.remove(judgeList.indexOf(b.getAuxiliary_serial_number_two()));
                }
            } else if (list.get(index) instanceof DepositGoodsEditInfo.DataBean.SerialListBean) {
                DepositGoodsEditInfo.DataBean.SerialListBean b = (DepositGoodsEditInfo.DataBean.SerialListBean) list.get(index);
                if (judgeList.indexOf(b.getMain_serial_number()) != -1) {
                    judgeList.remove(judgeList.indexOf(b.getMain_serial_number()));
                }
                if (judgeList.indexOf(b.getAuxiliary_serial_number_one()) != -1) {
                    judgeList.remove(judgeList.indexOf(b.getAuxiliary_serial_number_one()));
                }
                if (judgeList.indexOf(b.getAuxiliary_serial_number_two()) != -1) {
                    judgeList.remove(judgeList.indexOf(b.getAuxiliary_serial_number_two()));
                }
            }
            if (judgeList.indexOf(code.getText().toString()) != -1) {
                Toast.makeText(parent.getContext(), "主串碼重複", Toast.LENGTH_SHORT).show();
                return;
            }
            if (!TextUtils.isEmpty(code1.getText().toString()) && judgeList.indexOf(code1.getText().toString()) != -1) {
                Toast.makeText(parent.getContext(), "輔助串碼1重複", Toast.LENGTH_SHORT).show();
                return;
            }
            if (!TextUtils.isEmpty(code2.getText().toString()) && judgeList.indexOf(code2.getText().toString()) != -1) {
                Toast.makeText(parent.getContext(), "輔助串碼2重複", Toast.LENGTH_SHORT).show();
                return;
            }
            code.setEnabled(false);
            code1.setEnabled(false);
            code2.setEnabled(false);
            if (list.get(index) instanceof DepositGoodsEditInfo.DataBean.SerialListBean) {
                DepositGoodsEditInfo.DataBean.SerialListBean bean = (DepositGoodsEditInfo.DataBean.SerialListBean) list.get(index);
                if (bean.getEdit_status() == 1) {
                    viewHolder.codeDetailEdit.setVisibility(View.GONE);
                    viewHolder.codeDetailDelete.setVisibility(View.GONE);
                    viewHolder.codeDetailSave.setVisibility(View.GONE);
                } else if (bean.getEdit_status() == 2) {
                    viewHolder.codeDetailEdit.setVisibility(View.VISIBLE);
                    viewHolder.codeDetailDelete.setVisibility(View.GONE);
                    viewHolder.codeDetailSave.setVisibility(View.GONE);
                } else {
                    viewHolder.codeDetailEdit.setVisibility(View.VISIBLE);
                    viewHolder.codeDetailDelete.setVisibility(View.VISIBLE);
                    viewHolder.codeDetailSave.setVisibility(View.GONE);
                }
            } else {
                edit.setVisibility(View.VISIBLE);
                delete.setVisibility(View.VISIBLE);
                save.setVisibility(View.GONE);
            }
            if (list.get(index) instanceof ReportLossGoodBean.DataBean.ListBean.SerialBean) {
                ((ReportLossGoodBean.DataBean.ListBean.SerialBean) list.get(index)).setMain_serial_number(code.getText().toString());
                ((ReportLossGoodBean.DataBean.ListBean.SerialBean) list.get(index)).setAuxiliary_serial_number_one(code1.getText().toString());
                ((ReportLossGoodBean.DataBean.ListBean.SerialBean) list.get(index)).setAuxiliary_serial_number_two(code2.getText().toString());
            } else if (list.get(index) instanceof LossGoodsInfo.DataBean.SerialBean) {
                ((LossGoodsInfo.DataBean.SerialBean) list.get(index)).setMain_serial_number(code.getText().toString());
                ((LossGoodsInfo.DataBean.SerialBean) list.get(index)).setAuxiliary_serial_number_one(code1.getText().toString());
                ((LossGoodsInfo.DataBean.SerialBean) list.get(index)).setAuxiliary_serial_number_two(code2.getText().toString());
            } else if (list.get(index) instanceof DepositGoodsInfo.DataBean.ListBean.SerialBean) {
                ((DepositGoodsInfo.DataBean.ListBean.SerialBean) list.get(index)).setMain_serial_number(code.getText().toString());
                ((DepositGoodsInfo.DataBean.ListBean.SerialBean) list.get(index)).setAuxiliary_serial_number_one(code1.getText().toString());
                ((DepositGoodsInfo.DataBean.ListBean.SerialBean) list.get(index)).setAuxiliary_serial_number_two(code2.getText().toString());
            } else if (list.get(index) instanceof DepositGoodsEditInfo.DataBean.SerialListBean) {
                ((DepositGoodsEditInfo.DataBean.SerialListBean) list.get(index)).setMain_serial_number(code.getText().toString());
                ((DepositGoodsEditInfo.DataBean.SerialListBean) list.get(index)).setAuxiliary_serial_number_one(code1.getText().toString());
                ((DepositGoodsEditInfo.DataBean.SerialListBean) list.get(index)).setAuxiliary_serial_number_two(code2.getText().toString());
            }
//            operation.opera(3, index);
        });
        return convertView;
    }

    public interface Operation {
        void opera(int type, int position);
    }

    public void CallBack(Operation operation) {
        this.operation = operation;
    }

    static class ViewHolder {
        @BindView(R.id.codeDetail_code)
        EditText codeDetailCode;
        @BindView(R.id.codeDetail_code1)
        EditText codeDetailCode1;
        @BindView(R.id.codeDetail_code2)
        EditText codeDetailCode2;
        @BindView(R.id.codeDetail_edit)
        LinearLayout codeDetailEdit;
        @BindView(R.id.codeDetail_delete)
        LinearLayout codeDetailDelete;
        @BindView(R.id.codeDetail_save)
        LinearLayout codeDetailSave;
        @BindView(R.id.codeDetail_choice)
        LinearLayout codeDetailChoice;
        @BindView(R.id.codeDetail_selected)
        LinearLayout codeDetailSelected;
        @BindView(R.id.codeDetail_modify)
        TextView codeDetailModify;
        @BindView(R.id.codeDetail_record)
        TextView codeDetailRecord;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
